package com.itcast.service;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itcast.dao.MemberMapper;
import com.itcast.dao.OrderMapper;
import com.itcast.dao.OrderListMapper;
import com.itcast.dao.OrderSettingMapper;
import com.itcast.health.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author: Song
 * @version: V1.0
 * @date: 2019/12/3 11:18
 * @description: com.itcast.service
 */
@Service(interfaceClass = OrderListService.class)
@Transactional
public class OrderListServiceImpl implements OrderListService {

    @Autowired
    OrderListMapper orderListMapper;
    @Autowired
    OrderSettingMapper orderSettingMapper;
    @Autowired
    MemberMapper memberMapper;
    @Autowired
    OrderMapper orderMapper;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page page = PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());

        List<OrderListBean> orderLists = orderListMapper.findPage(queryPageBean.getQueryString());

        return new PageResult(page.getTotal(),orderLists);
    }

    @Override
    public Result add(OrderInfoVo orderInfoVo) {
        Date orderDate = orderInfoVo.getOrderDate();
        String telephone = orderInfoVo.getTelephone();
        String idCard = orderInfoVo.getIdCard();
        String name = orderInfoVo.getName();
        Integer sex = orderInfoVo.getSex();
        Integer setmealId = orderInfoVo.getSetmealId();
        String orderType = orderInfoVo.getOrderType();

        // 1 、根据预约时间查询是否存在预约设置(返回一个预约设置对象)
        OrderSetting orderSetting = orderSettingMapper.findByDate(orderDate);
        if(null == orderSetting){
            return Result.error("没有档期");//这个提示信息是文案需要产品提供
        }
        // 2、校验预约是否已满
        int number = orderSetting.getNumber();//预约总数
        int reservations = orderSetting.getReservations();//已预约数

        if(number <= reservations){
            return Result.error("预约已满");
        }
        //3、根据手机号码查询是否是我们的会员
        Member member = memberMapper.findByTelephone(telephone);
        //4、如果不是就自动注册
        if(null == member){
            member = new Member();
            member.setPhoneNumber(telephone);
            member.setIdCard(idCard);
            member.setSex(String.valueOf(sex));
            member.setName(name);
            member.setRegTime(new Date());
            memberMapper.add(member);
        }

        Integer memberId = member.getId();
        //5、根据会员id，套餐id，预约时间查询是否重复预约
        Order queryOrder = new Order();
        queryOrder.setSetmealId(setmealId);
        queryOrder.setOrderDate(orderDate);
        queryOrder.setMemberId(memberId);
        List<Order> orders = orderMapper.findByCondition(queryOrder);
        if(CollectionUtil.isNotEmpty(orders)){
            return Result.error("不要重复预约");
        }
        //6、 直接下单
        Order order = new Order();
        order.setMemberId(memberId);
        order.setOrderDate(orderDate);
        order.setOrderType(orderType);
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setSetmealId(setmealId);
        orderMapper.add(order);
        //7、修改已预约人数 + 1
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        int i = orderSettingMapper.editReservations(orderSetting);
        if(i == 0){
            //没有更新成功
            throw  new RuntimeException("更新失败");
        }
        return Result.success("添加预约成功");
    }

    @Override
    public List<Setmeal> findAll() {
        return orderListMapper.findAll();
    }
}
